﻿using System;
using System.Linq;
using System.Web.Mvc;
using BLL;
using Model;

namespace Web.Areas.Manager.Controllers
{
    [NoCache]
    [UserAuthorize]
    [HandleError]
    public class Information_ContentController : Controller
    {
        Information_Content_BLL _Information_Content_BLL = new Information_Content_BLL();

        public virtual ActionResult Index(int? pageIndex, string keyword)
        {
            var model = _Information_Content_BLL.Get();
            if (!string.IsNullOrEmpty(keyword))
            {
                model = model.Where(a => a.Title.Contains(keyword));
            }
            return View(model.ToPagedList(pageIndex));
        }

        public virtual ActionResult Details(Guid id)
        {
            var item = _Information_Content_BLL.Get(id);
            return View(item);
        }

        public virtual ActionResult Edit(Guid? id)
        {
            //显示现有
            Information_Channel_BLL _Information_Channel_BLL = new Information_Channel_BLL();
            ViewData["Information_Channel"] = _Information_Channel_BLL.Get();
            var item = _Information_Content_BLL.Get(id);
            return View(item);
        }

        [HttpPost]
        [ValidateInput(false)]
        public virtual ActionResult Edit(Guid? id, Information_Content collection)
        {
            Edit(id);

            if (!ModelState.IsValid)
            {
                return View();
            }

            Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();

            collection.UserId = _Sys_Users_BLL.Get(User.Identity.Name).Id;

            _Information_Content_BLL.Save(id, ref collection);
            return RedirectToAction("Index", RouteData.Values);
        }

        [HttpDelete]
        //id可以为多个以逗号分隔
        public ActionResult Delete(string id)
        {
            string[] ids = id.Split(',');

            foreach (var item in ids)
            {
                try
                {
                    _Information_Content_BLL.DeleteObject(new Guid(item));
                }
                catch { }
            }

            return RedirectToAction("Index", RouteData.Values);
        }

        //[HttpDelete]
        //public virtual ActionResult Delete(Guid id)
        //{
        //    _Information_Content_BLL.DeleteObject(id);
        //    return RedirectToAction("Index", RouteData.Values);
        //}
    }
}
